
# Replication Materials for Electoral Confidence, Overconfidence, and Risky Behavior: Evidence From a Study With Elected Politicians
# ==================================================================================================================================

## Authors: Lior Sheffer and Peter Loewen
## liorsheffer@gmail.com; peter.loewen@utoronto.ca

## This replication code uses a version of the original data that omits year of birth information on 
## respondents. This is required in order to preserve respondent anonymity. Models reported in the 
## paper and in the online appendix generally use year of birth as a control. The original models are 
## available in "oc_paper_politicalbehaviour_replication.R".


library(stargazer)
library(foreign)
library(reshape)
library(car)
library(dplyr)
library(tidyr)
library(ggplot2)
library(cowplot)
library(scales)
library(grid)
library(Zelig)

rm(list = ls())

country_names <- c(
  `1`="Belgium",
  `2`="Canada",
  `3`="Israel"
)

country_labeller <- function(variable,value){
  return(country_names[value])
}


##oc_sample <- read.csv("oc_paper_replication_materials.csv", header = TRUE)
oc_sample <- read.csv("oc_paper_replication_materials_noyob.csv", header = TRUE)


## Tables and Figures from Paper
## =============================


## Table 2

mean(oc_sample$rc_01)
sd(oc_sample$rc_01)
aggregate(rc_01~country,oc_sample,mean)
aggregate(rc_01~country,oc_sample,sd)

mean(oc_sample$es_01)
sd(oc_sample$es_01)
aggregate(es_01~country,oc_sample,mean)
aggregate(es_01~country,oc_sample,sd)

mean(oc_sample$oc)
sd(oc_sample$oc)
aggregate(oc~country,oc_sample,mean)
aggregate(oc~country,oc_sample,sd)

mean(oc_sample$oc_01)
sd(oc_sample$oc_01)
aggregate(oc_01~country,oc_sample,mean)
aggregate(oc_01~country,oc_sample,sd)

mean(oc_sample$es_01_p)
sd(oc_sample$es_01_p)
aggregate(es_01_p~country,oc_sample,mean)
aggregate(es_01_p~country,oc_sample,sd)

mean(oc_sample$oc_p)
sd(oc_sample$oc_p)
aggregate(oc_p~country,oc_sample,mean)
aggregate(oc_p~country,oc_sample,sd)

mean(oc_sample$oc_01_p)
sd(oc_sample$oc_01_p)
aggregate(oc_01_p~country,oc_sample,mean)
aggregate(oc_01_p~country,oc_sample,sd)


## Table 3

m1 <- glm(asianbinary ~ rc_01 + sex + country, data=oc_sample,family=binomial(link="logit"))
summary(m1)
oc_sample$m1pred <- NULL
oc_sample$m1pred<-predict.glm(m1,oc_sample,type="response")

m2 <- glm(asianbinary ~ rc_01 + es_01 + sex + country, data=oc_sample,family=binomial(link="logit"))
summary(m2)
oc_sample$m2pred <- NULL
oc_sample$m2pred<-predict.glm(m2,oc_sample,type="response")


m2_p <- glm(asianbinary ~ rc_01 + es_01_p + sex  + country, data=oc_sample,family=binomial(link="logit"))
summary(m2_p)
oc_sample$m2_pred_p <- NULL
oc_sample$m2_pred_p<-predict.glm(m2_p,oc_sample,type="response")

stargazer(m1,m2,m2_p,
          title="Regression Results",
          column.labels = c("Model 1","Model 2 (ES: Election-Based)","Model 2 (ES: Polling-Based)"),
          no.space=TRUE)

  ## (Psuedo R2 for Table 3 Models)
  library(pscl)
    pR2(m1)
    pR2(m2)
    pR2(m2_p)
    
    pR2(m2_temp3)
    m2_temp4 <- glm(asianbinary ~ oc_01 + sex + country, data=oc_sample,family=binomial(link="logit"))
    m2_temp5 <- glm(asianbinary ~ es_01 + sex + country, data=oc_sample,family=binomial(link="logit"))
    pR2(m2_temp4)
    pR2(m2_temp5)

  
## Figure 1

forplotR <- dplyr::select(oc_sample,c(asianbinary,asianmean,m1pred,m2pred,m2_pred_p,rc_01,oc,oc_p,oc_01,oc_01_p,es_01,es_01_p,loss_frame,acc_frame,sex,countrybinary,margin2011,margin2015r,voteshare2011,voteshare2015r))
  
    rc_plot <- ggplot(data=forplotR,aes(rc_01,m1pred))+
      geom_point()+
      labs(x = "Self-Reported Confidence in Re-Election", y = "Risk Taking (Model I Predicted Likelihood)") +
      theme_grey() +
      theme(axis.text=element_text(size=16),axis.title.x=element_text(size=16,margin=margin(15,0,0,0)),
            axis.title.y=element_text(size=16,margin=margin(0,15,0,0))) +
      ylim(0.2,0.8)
    
    rc_plot
    ggsave("rc_plotR_v3.eps",width=6,height=5)
    
    
## Figure 2
    
    oc_plot <- ggplot(data=forplotR,aes(oc_01,m2pred))+
      geom_point() +
      labs(x = "Overconfidence",y = "Risk Taking (Model II Predicted Likelihood)")+
      theme_grey() +
      theme(axis.text=element_text(size=16),axis.title.x=element_text(size=16,margin=margin(15,0,0,0)),
            axis.title.y=element_text(size=16,margin=margin(0,15,0,0))) +
      geom_smooth(method="lm",size=1,color="gray50")
    
    oc_plot
    
    es_plot <- ggplot(data=forplotR,aes(es_01,m2pred))+
      geom_point() +
      labs(x = "Electoral Safety",y = "") +
      theme_grey() +
      theme(axis.text=element_text(size=16),axis.title.x=element_text(size=16,margin=margin(15,0,0,0)),
            axis.title.y=element_text(size=16,margin=margin(0,15,0,0))) +
      geom_smooth(method="lm",size=1,color="gray50")
    
    es_plot
    
    require(cowplot)
    oc_es_plot <- plot_grid(oc_plot, NULL, es_plot, nrow=1,align='h',rel_widths = c(1,0.01,1.05))
    oc_es_plot
    ggsave("oc_es_plotR_v3.eps",width=10,height=5)
    ggsave("oc_es_plotR_v3.pdf",width=10,height=5)
    
    
## Figure 3

    oc_plot_p <- ggplot(data=forplotR,aes(oc_01_p,m2_pred_p))+
      geom_point() +
      labs(x = "Overconfidence",y = "Risk Taking (Model II Predicted Likelihood)")+
      theme_grey() +
      theme(axis.text=element_text(size=16),axis.title.x=element_text(size=16,margin=margin(15,0,0,0)),
            axis.title.y=element_text(size=16,margin=margin(0,15,0,0))) +
      geom_smooth(method="lm",size=1,color="gray50")
    
    oc_plot_p
    
    es_plot_p <- ggplot(data=forplotR,aes(es_01_p,m2_pred_p))+
      geom_point() +
      labs(x = "Electoral Safety",y = "") +
      theme_grey() +
      theme(axis.text=element_text(size=16),axis.title.x=element_text(size=16,margin=margin(15,0,0,0)),
            axis.title.y=element_text(size=16,margin=margin(0,15,0,0))) +
      geom_smooth(method="lm",size=1,color="gray50")
    
    es_plot_p
    
    require(cowplot)
    oc_es_plot_p <- plot_grid(oc_plot_p, NULL, es_plot_p, nrow=1,align='h',rel_widths = c(1,0.01,1.05))
    oc_es_plot_p
    ggsave("oc_es_plotR_p_v3.eps",width=10,height=5)
    ggsave("oc_es_plotR_p_v3.pdf",width=10,height=5)
    
    
  ## Tables and Figures for Online Appendix
  ## ======================================
    
  
  ## Table 2
    
    m_rc <- lm(rc_01 ~ loss_frame + acc_frame, data=oc_sample)
    summary(m_rc)
    
    m_oc <- lm(oc ~ loss_frame + acc_frame, data=oc_sample)
    summary(m_oc)
    
    m_es <- lm(es_01 ~ loss_frame + acc_frame, data=oc_sample)
    summary(m_es)
    
    stargazer(m_rc,m_oc,m_es,title="Dependent Variable:",
              covariate.labels = c("Loss Frame","High Accountability"),
              no.space=TRUE)
    
    
  ## Table 5
  
  m1_country <- glm(asianbinary ~ rc_01*country + sex , data=oc_sample,family=binomial(link="logit"))
  summary(m1_country)
  
  m2_country_oc <- glm(asianbinary ~ rc_01*country + es_01 + sex , data=oc_sample,family=binomial(link="logit"))
  summary(m2_country_oc)
  
  m2_country_es <- glm(asianbinary ~ rc_01 + es_01*country + sex , data=oc_sample,family=binomial(link="logit"))
  summary(m2_country_es)


  m1_p_country <- glm(asianbinary ~ rc_01*country + sex , data=oc_sample,family=binomial(link="logit"))
  summary(m1_p_country)
  
  m2_p_country_oc <- glm(asianbinary ~ rc_01*country + es_01_p + sex , data=oc_sample,family=binomial(link="logit"))
  summary(m2_p_country_oc)
  
  m2_p_country_es <- glm(asianbinary ~ rc_01 + es_01_p*country + sex , data=oc_sample,family=binomial(link="logit"))
  summary(m2_p_country_es)
  
  stargazer(m1_country,m2_country_oc,m2_country_es,
            m2_p_country_oc,m2_p_country_es,
            title="Regression Results",
            column.labels = c("Model 1","Model 2A","Model 2B","Model 2A","Model 2B"),
            no.space=TRUE)
  
  
## Table 6
  
  m1_treatment <- glm(asianbinary ~ rc_01 + sex + country + loss_frame + acc_frame, data=oc_sample,family=binomial(link="logit"))
  summary(m1_treatment)
  
  m2_treatment <- glm(asianbinary ~ rc_01 + es_01 + sex + country + loss_frame + acc_frame, data=oc_sample,family=binomial(link="logit"))
  summary(m2_treatment)
  
  m1_p_treatment <- glm(asianbinary ~ rc_01 + sex + country + loss_frame + acc_frame, data=oc_sample,family=binomial(link="logit"))
  summary(m1_treatment)
  
  m2_p_treatment <- glm(asianbinary ~ rc_01 + es_01_p + sex + country + loss_frame + acc_frame, data=oc_sample,family=binomial(link="logit"))
  summary(m2_p_treatment)
  
  stargazer(m1_treatment,m2_treatment,m2_p_treatment,
            title="Regression Results",
            column.labels = c("Model 1","Model 2 (ES: Election-Based)","Model 2 (ES: Polling-Based)"),
            no.space=TRUE)
  
  
  ## Table 7
  
  m2_treatment_interaction1 <- glm(asianbinary ~ rc_01*es_01 + sex  + country, data=oc_sample,family=binomial(link="logit"))
  summary(m2_treatment_interaction1)
  
  m2_p_treatment_interaction1 <- glm(asianbinary ~ rc_01*es_01_p + sex  + country, data=oc_sample,family=binomial(link="logit"))
  summary(m2_p_treatment_interaction1)
  
  
  stargazer(m2_treatment_interaction1,m2_p_treatment_interaction1,title="Regression Results",
            column.labels = c("Model 2 (ES: Election-Based)","Model 2 (ES: Polling-Based)")
            , no.space=TRUE)
  
  
  ## Table 8
  
  oc_sample$es_re <- oc_sample$reelected
  oc_sample$oc_re <- oc_sample$rc_01 - oc_sample$es_re
  
  if ( max(oc_sample$oc_re,na.rm=TRUE) - min(oc_sample$oc_re,na.rm=TRUE) == 0) {
    oc_sample$oc_re_01 <- NA
  } else {
    oc_sample$oc_re_01 <- (oc_sample$oc_re - min(oc_sample$oc_re,na.rm=TRUE))/(max(oc_sample$oc_re,na.rm=TRUE)-min(oc_sample$oc_re,na.rm=TRUE))
  }
  
  m2_re <- glm(asianbinary ~ rc_01 + es_re + sex  + country, data=oc_sample,family=binomial(link="logit"))
  summary(m2_re)
  
  stargazer(m2_re,title="Regression Results",
            covariate.labels = c("Overconfidence","Electoral Safety","Female","Year of Birth","Canada (FE)","Israel (FE)","Intercept")
            , no.space=TRUE)
  
  
  ## Figure 1
  
  rc_plot.raw <- ggplot(data=forplotR,aes(rc_01,asianbinary))+
    geom_jitter(height=0.01,width=0.1)+
    geom_smooth(method=lm, colour="black") +
    labs(x = "Self-Reported Confidence in Re-Election", y = "Risk Taking") +
    theme_grey() 
  rc_plot.raw
  ggsave("rc_plotR_raw_v3.eps",width=6,height=5)
  ggsave("rc_plotR_raw_v3.pdf",width=6,height=5)
  
  
  ## Figure 2
  
  es_plot.raw <- ggplot(data=forplotR,aes(es_01,asianbinary))+
    geom_jitter(height=0.01)+
    geom_smooth(method=lm, colour="black") +
    labs(x = "Electoral Safety", y = "Risk Taking") +
    theme_grey() 
  es_plot.raw
  ggsave("es_plotR_raw_v3.eps",width=6,height=5)
  ggsave("es_plotR_raw_v3.pdf",width=6,height=5)
  
  
  ## Figure 3
  
  oc_plot.raw <- ggplot(data=forplotR,aes(oc_01,asianbinary))+
    geom_jitter(height=0.01)+
    geom_smooth(method=lm, colour="black") +
    labs(x = "Overconfidence", y = "Risk Taking") +
    theme_grey() 
  oc_plot.raw
  ggsave("oc_plotR_raw_v3.eps",width=6,height=5)
  ggsave("oc_plotR_raw_v3.pdf",width=6,height=5)
  
  
  ## Figure 4
  
  rc_plot_countries <- ggplot(data=forplotR,aes(rc_01,m1pred))+
    geom_point() +
    labs(x = "Confidence in Re-Election",y = "Risk Taking (Model I Predicted Likelihood)")+
    theme_grey() +
    theme(axis.text=element_text(size=16),axis.title.x=element_text(size=16,margin=margin(15,0,0,0)),
          axis.title.y=element_text(size=16,margin=margin(0,15,0,0)),
          strip.text.x = element_text(size=16),
          panel.margin = unit(1, "lines")) +
    
    ylim(0.2,1) +
    scale_y_continuous(limits=c(0.2,1),breaks=seq(0.2,1,by = 0.2)) +
    facet_grid( ~countrybinary, labeller = as_labeller(country_labeller))
  
  rc_plot_countries 
  ggsave("rc_country_plotR_v3.eps",width=10,height=5)
  ggsave("rc_country_plotR_v3.pdf",width=10,height=5)
  
  
  ## Figure 5
  
  oc_plot_countries <- ggplot(data=forplotR,aes(oc_01,m2pred))+
    geom_point() +
    labs(x = "Overconfidence",y = "Risk Taking (Model II Predicted Likelihood)")+
    theme_grey() +
    theme(axis.text=element_text(size=16),axis.title.x=element_text(size=16,margin=margin(15,0,0,0)),
          axis.title.y=element_text(size=16,margin=margin(0,15,0,0)),
          strip.text.x = element_text(size=16),
          panel.margin = unit(1, "lines")) +
    geom_smooth(method="lm",size=1,color="gray50")+
    ylim(0.2,1) +
    scale_y_continuous(limits=c(0.2,1),breaks=seq(0.2,1,by = 0.2)) +
    facet_grid( ~countrybinary, labeller = as_labeller(country_labeller))
  
  oc_plot_countries 
  ggsave("oc_country_plotR_v3.eps",width=10,height=5)
  ggsave("oc_country_plotR_v3.pdf",width=10,height=5)
  
  
  ## Figure 6
  
  es_plot_countries <- ggplot(data=forplotR,aes(es_01,m2pred))+
    geom_point() +
    labs(x = "Electoral Safety",y = "Risk Taking (Model II Predicted Likelihood)") +
    theme_grey() +
    theme(axis.text=element_text(size=16),axis.title.x=element_text(size=16,margin=margin(15,0,0,0)),
          axis.title.y=element_text(size=16,margin=margin(0,15,0,0)),
          strip.text.x = element_text(size=16),
          panel.margin = unit(1, "lines")) +
    geom_smooth(method="lm",size=1,color="gray50") +
    ylim(0.2,1) +
    scale_y_continuous(limits=c(0.2,1),breaks=seq(0.2,1,by = 0.2)) +
    facet_grid( ~countrybinary, labeller = as_labeller(country_labeller))
  
  es_plot_countries 
  ggsave("es_country_plotR_v3.eps",width=10,height=5)
  ggsave("es_country_plotR_v3.pdf",width=10,height=5)
  
  
  ## Figure 7
  
  oc_plot_countries_p <- ggplot(data=forplotR,aes(oc_01_p,m2pred))+
    geom_point() +
    labs(x = "Overconfidence",y = "Risk Taking (Model II Predicted Likelihood)")+
    theme_grey() +
    theme(axis.text=element_text(size=16),axis.title.x=element_text(size=16,margin=margin(15,0,0,0)),
          axis.title.y=element_text(size=16,margin=margin(0,15,0,0)),
          strip.text.x = element_text(size=16),
          panel.margin = unit(1, "lines")) +
    geom_smooth(method="lm",size=1,color="gray50")+
    ylim(0.2,1) +
    scale_y_continuous(limits=c(0.2,1),breaks=seq(0.2,1,by = 0.2)) +
    facet_grid( ~countrybinary, labeller = as_labeller(country_labeller))
  
  oc_plot_countries_p 
  ggsave("oc_country_p_plotR_v3.eps",width=10,height=5)
  ggsave("oc_country_p_plotR_v3.pdf",width=10,height=5)
  
  
  ## Figure 8
  
  es_plot_countries_p <- ggplot(data=forplotR,aes(es_01_p,m2pred))+
    geom_point() +
    labs(x = "Electoral Safety",y = "Risk Taking (Model II Predicted Likelihood)") +
    theme_grey() +
    theme(axis.text=element_text(size=16),axis.title.x=element_text(size=16,margin=margin(15,0,0,0)),
          axis.title.y=element_text(size=16,margin=margin(0,15,0,0)),
          strip.text.x = element_text(size=16),
          panel.margin = unit(1, "lines")) +
    geom_smooth(method="lm",size=1,color="gray50") +
    ylim(0.2,1) +
    scale_y_continuous(limits=c(0.2,1),breaks=seq(0.2,1,by = 0.2)) +
    facet_grid( ~countrybinary, labeller = as_labeller(country_labeller))
  
  es_plot_countries_p 
  ggsave("es_country_p_plotR_v3.eps",width=10,height=5)
  ggsave("es_country_p_plotR_v3.pdf",width=10,height=5)
  
  
  ## Figure 9
  
  rc_plot_colour <- ggplot(data=forplotR,aes(rc_01,m1pred,colour=factor(loss_frame)))+
    geom_point()+
    labs(x = "Self-Reported Confidence in Re-Election", y = "Risk Taking (Model I Predicted Likelihood)") +
    theme_grey() +
    theme(axis.text=element_text(size=16),axis.title.x=element_text(size=16,margin=margin(15,0,0,0)),
          axis.title.y=element_text(size=16,margin=margin(0,15,0,0))) +
    guides(color=FALSE) +
    ylim(0.2,0.8)
  
  rc_plot_colour
  ggsave("rc_plotR_colour.eps",width=6,height=5)
  
  
  ## Figure 10
  
  oc_plot_colour <- ggplot(data=forplotR,aes(oc,m2pred,colour=factor(loss_frame)))+
    geom_point() +
    labs(x = "Overconfidence",y = "Risk Taking (Model II Predicted Likelihood)")+
    theme_grey() +
    theme(axis.text=element_text(size=16),axis.title.x=element_text(size=16,margin=margin(15,0,0,0)),
          axis.title.y=element_text(size=16,margin=margin(0,15,0,0))) +
    guides(color=FALSE) +
    geom_smooth(method="lm",size=1,color="gray50")
  
  oc_plot_colour
  
  es_plot_colour <- ggplot(data=forplotR,aes(es_01,m2pred,colour=factor(loss_frame)))+
    geom_point() +
    labs(x = "Electoral Safety",y = "") +
    theme_grey() +
    theme(axis.text=element_text(size=16),axis.title.x=element_text(size=16,margin=margin(15,0,0,0)),
          axis.title.y=element_text(size=16,margin=margin(0,15,0,0))) +
    guides(color=FALSE) +
    geom_smooth(method="lm",size=1,color="gray50")
  
  es_plot_colour
  
  require(cowplot)
  oc_es_plot_colour <- plot_grid(oc_plot_colour, NULL, es_plot_colour, nrow=1,align='h',rel_widths = c(1,0.01,1.05))
  oc_es_plot_colour
  ggsave("oc_es_plotR_colour.eps",width=10,height=5)
  ggsave("oc_es_plotR_colour.pdf",width=10,height=5)
  
  
  ## Figure 11
  
  oc_plot_colour_p <- ggplot(data=forplotR,aes(oc_p,m2_pred_p,colour=factor(loss_frame)))+
    geom_point() +
    labs(x = "Overconfidence",y = "Risk Taking (Model II Predicted Likelihood)")+
    theme_grey() +
    theme(axis.text=element_text(size=16),axis.title.x=element_text(size=16,margin=margin(15,0,0,0)),
          axis.title.y=element_text(size=16,margin=margin(0,15,0,0))) +
    guides(color=FALSE) +
    geom_smooth(method="lm",size=1,color="gray50")
  
  oc_plot_colour_p
  
  es_plot_colour_p <- ggplot(data=forplotR,aes(es_01_p,m2_pred_p,colour=factor(loss_frame)))+
    geom_point() +
    labs(x = "Electoral Safety",y = "") +
    theme_grey() +
    theme(axis.text=element_text(size=16),axis.title.x=element_text(size=16,margin=margin(15,0,0,0)),
          axis.title.y=element_text(size=16,margin=margin(0,15,0,0))) +
    guides(color=FALSE) +
    geom_smooth(method="lm",size=1,color="gray50")
  
  es_plot_colour_p
  
  require(cowplot)
  oc_es_plot_colour_p <- plot_grid(oc_plot_colour_p, NULL, es_plot_colour_p, nrow=1,align='h',rel_widths = c(1,0.01,1.05))
  oc_es_plot_colour_p
  ggsave("oc_es_plotR_colour_p.eps",width=10,height=5)
  ggsave("oc_es_plotR_colour_p.pdf",width=10,height=5)
  
  
  ## Table 9
  
  library(pscl)
  
  m_reelected_1 <- glm(reelected ~ oc + es_01, data=oc_sample,family=binomial(link="logit"))
  summary(m_reelected_1)
  
  m_reelected_2 <- glm(reelected ~ oc + es_01 + country, data=oc_sample,family=binomial(link="logit"))
  summary(m_reelected_2)
  
  m_polled_1 <- lm(es_01_p ~ oc + es_01, data=oc_sample)
  summary(m_polled_1)
  
  m_polled_2 <- lm(es_01_p ~ oc + es_01 + country, data=oc_sample)
  summary(m_polled_2)
  
  stargazer(m_reelected_1,m_reelected_2,m_polled_1,m_polled_2,title="Regression Results",
            column.labels = c("Re-Elected (1)","Re-Elected (2)","Polling (1)","Polling (2)"),
            covariate.labels = c("Overconfidence","Electoral Safety","Canada (FE)","Israel (FE)","Intercept")
            ,no.space=TRUE)
  